import { fetchMonitorData, fetchTimeRange } from './service';
import { formatterMon,getCurrentDate } from '@/utils/utils'

const Model = {
  namespace: 'rentMonitor',
  state: {
    dealData: {},
    stockPeopleData: {},
    tabKey: '1',
    region: '大理州',
    // originCode:'530000',
    showPic:true,
    region_code: '530000',
    level: 'city',
    purpose: '存量房',
    staticts: '面积',
    date: '2019-10-1',
    defaultDate: [],//默认值
    dateRange: [],//time_value
    seqRange: [],
    currentDate: getCurrentDate(),//当前日期（用于渲染表格）
    currentDates: [],//当前日期（ranger）（用于渲染表格）
    currentSeq: 0,//当前seq
    currentSeqs: []//当前seqs（ranger）   currentSeqs.length=2   [seq1,seq2]
  },
  effects: {
    *fetchCommerBuildData({ payload = 'rent' }, { call, put, select }) {
      const { level, purpose, region, region_code, currentSeqs = [133, 166],currentSeq=133 } = yield select(state => state.rentMonitor)
      // console.log(region)
      const sliceRegion = region.length > 5 ? region.slice(0, 2) + region.slice(-1) : region
      const nameMapping = {  rent: 'dealData', rentPeople: 'stockPeopleData' }
      const response = yield call(fetchMonitorData, payload,
        { level: level, time_type: '月度', region_id: region_code, purpose_name: purpose, seq: currentSeq, start_seq: currentSeqs[0], end_seq: currentSeqs[1] }
      );
      yield put({
        type: 'saveChartData',
        payload: response,
        module: nameMapping[payload]
      });
      yield put({
        type: 'changeShowPic',
        payload: true
      })
    },
    *fetchTimeRange({ payload }, { call, put }) {
      const response = yield call(fetchTimeRange, payload)
      yield put({
        type: 'saveDateRange',
        payload: response
      })
    },
    //针对hotmap点击以及返回请求数据方法
    *fetchMapQuery({ payload }, { call, put,select }) {
      yield put({
        type: 'saveCurrentRegion',
        payload: payload.data
      })
      const { level, purpose, region, region_code, currentSeqs = [133, 166], currentSeq = 133 } = yield select(state => state.rentMonitor)
      const nameMapping = {  rent: 'dealData', rentPeople: 'stockPeopleData' }
      const response = yield call(fetchMonitorData, payload.type,
        { level, time_type: '月度', region_id: region_code, purpose_name: purpose, seq: currentSeq, start_seq: currentSeqs[0], end_seq: currentSeqs[1] }
      );
      yield put({
        type: 'saveChartData',
        payload: response,
        module: nameMapping[payload.type]
      });
    }
  },
  reducers: {
    saveChartData(state, action) {
      return { ...state, [action.module]: action.payload };
    },
    changeTabs(state, action) {
      return { ...state, tabKey: action.payload };
    },
    // 修改行政区
    changeRegion(state, action) {
      const { region, region_code,level } = action.payload
      return { ...state, region: region, region_code: region_code,level:level };
    },
    // 修改物业类型选择
    changePurpose(state, action) {
      return { ...state, purpose: action.payload };
    },
    // 修改分析维度选择
    changeStaticts(state, action) {
      return { ...state, staticts: action.payload };
    },
    // 修改时间选择
    changeQueryDate(state, action) {
      return { ...state, date: action.payload };
    },
    changeShowPic(state, action) {
      return { ...state, showPic: action.payload };
    },
    //保存时间信息并格式化处理(请求得到)
    saveDateRange(state, action) {
      const dates = action.payload
      const dateArr = []
      const defaultValue = []
      const seqArr = []
      dates.map((item, index) => {
        dateArr.push(item.time_value)
        seqArr.push(item.seq)
        if (index == 0 || index == dates.length - 1) {//moment格式化默认值
          const newStr = formatterMon(item.time_value)
          defaultValue.push(newStr)
        }
      })

      return {
        ...state,
        defaultDate: defaultValue,
        dateRange: dateArr, seqRange: seqArr,
        currentDate: defaultValue[1],
        currentSeq: seqArr[seqArr.length - 1],
        currentDates: defaultValue,
        currentSeqs: [seqArr[0], seqArr[seqArr.length - 1]]
      }
    },
    //onchange触发后进行时间信息修改   type:date
    saveChangedDate(state, action) {
      const { currentDate, currentSeq } = action.payload
      return {
        ...state,
        currentDate: currentDate,
        currentSeq: currentSeq
      }
    },
    saveChangedDates(state, action) {
      const { currentDates, currentSeqs } = action.payload
      return {
        ...state,
        currentDates: currentDates,
        currentSeqs: currentSeqs
      }
    },
    saveCurrentRegion(state, action){
      console.log(action.payload)
      const{adcode,name,level}=action.payload
      return{
        ...state,
        region:name,
        region_code:adcode,
        level
      }
    },

  },
};
export default Model;
